package com.granada.model.common;

import com.granada.common.Curd;
import com.granada.entity.*;
import com.granada.model.common.base.UserBase;
import com.granada.vo.resp.PageVO;
import com.granada.entity.*;
import com.granada.utils.MapperBeanUtils;
import com.granada.utils.PasswordUtils;

import java.util.ArrayList;
import java.util.List;

public abstract  class User<
        T extends SysUser,
        EW extends SysUserWechatRelation,
        W extends SysUserWechat,
        F extends Page
        > extends UserBase<T,EW,W,F> implements Curd {
    public T vo;
    public EW ewo;
    public W wo;
    public F fo;
    public Ao ao;



    public  User(T vo,  EW ewo, W wo, F fo, Ao ao) {
        this.vo = vo;

        this.ewo = ewo;
        this.wo = wo;
        this.fo = fo;
        this.ao = ao;
    }

    public void clear(){
        this.vo = null;
        this.ewo = null;
        this.wo = null;
        this.fo = null;
        this.ao = null;
    }

    @Override
    public Long add() throws Exception {
        ao.id = this.addSysUserBefore();
        this.addSysUserAfter();

        if (wo != null){
            ao.wechatUserId = this.addSysUserWechatBefore();
            this.addSysUserWechatAfter();

            this.addSysUserWechatRelationBefore();
            this.addSysUserWechatRelationAfter();
        }

        return ao.id;
    }

    public  void update() throws Exception {
        if(vo != null){
            String salt = PasswordUtils.getSalt();
            this.updateSysUserBefore();
            this.updateSysUserAfter();
        }

        if(wo != null){
            this.updateSysUserWechatBefore();
            this.updateSysUserWechatAfter();
        }

        if(ewo != null){
            this.updateSysUserWechatRelationBefore();
            this.updateSysUserWechatRelationAfter();
        }
    }

    @Override
    public final void delete() throws Exception {
        List<Long> ids = new ArrayList<>();
        ids.add(ao.getId());
        MapperBeanUtils.getSysUserMapper().deletedUsers(ids);
//        this.deleteSysUserBefore();
//        this.deleteSysUserAfter();
    }
    @Override
    public final void batchDelete() throws Exception{
        MapperBeanUtils.getSysUserMapper().deletedUsers(ao.getIds());
    }

    public T getOne() throws Exception {
        return (T) MapperBeanUtils.getSysUserMapper().selectByPrimaryKey(ao.id);
    }
    public T getSearchOne() throws Exception {
        return null;
    }
    public List<T> getAll() throws Exception {
        return null;
    }
    public List<T> getSearchAll() throws Exception {
        return null;
    }
    public abstract PageVO<T> getList() throws Exception;
    public PageVO<T> getSearchList() throws Exception {
        return null;
    }


    public abstract Long addSysUserBefore() throws Exception;
    public abstract void addSysUserAfter() throws Exception;


    public abstract Long addSysUserWechatBefore() throws Exception;
    public abstract void addSysUserWechatAfter() throws Exception;

    public Long addSysUserWechatRelationBefore() throws Exception{
        SysUserWechatRelation sysUserWechatRelation = SysUserWechatRelation.builder()
                .userId(ao.id)
                .userWechatId(ao.wechatUserId)
                .build();
        return MapperBeanUtils.getSysUserWechatRelationMapper().insert(sysUserWechatRelation);
    }
    public abstract void addSysUserWechatRelationAfter() throws Exception;


    public abstract void updateSysUserBefore() throws Exception;
    public abstract void updateSysUserAfter() throws Exception;


    public abstract void updateSysUserWechatBefore() throws Exception;
    public abstract void updateSysUserWechatAfter() throws Exception;
    public abstract void updateSysUserWechatRelationBefore() throws Exception;
    public abstract void updateSysUserWechatRelationAfter() throws Exception;
}
